
$(function() {
    var module = {};
    var tools = Tools();
    var goInvest=tools.goInvest;

    module.init=function () {
        var changeNum=0;
        tools.isOpenAccount();
        tools.getUserInfo();
        $('.myRedPage').on('click',function () {
            $('.myCoupon_info').hide();
            $('.myRedPage_info').show();
            module.initData('UNUSED','pager',false,tools.apiURL.redEnvelopeList,'investList');
            $('.USED_a,.EXPIRED_a').removeClass('select');
            $('.UNUSED_a').addClass('select');
            $('.myRedPage_a').addClass('curr');
            $('.myCoupon_a').removeClass('curr');
            $('.investList,.redp_s').show();
            $('.investList1,.coupon_s').hide();
        });
        $('.myCoupon').on('click',function () {
            $('.myRedPage_info').hide();
            $('.myCoupon_info').show();
            changeNum++;
            if(changeNum==1){
                tools.doGet(activityURL + tools.apiURL.interestCouponList, {status: "1",pageNumber: "1",pageSize: "1"}, module.totalCoupon1);
                tools.doGet(activityURL + tools.apiURL.interestCouponList, {status: "2",pageNumber: "1",pageSize: "1"}, module.totalCoupon2);
                tools.doGet(activityURL + tools.apiURL.interestCouponList, {status: "3",pageNumber: "1",pageSize: "1"}, module.totalCoupon3);
            }
            module.initData('UNUSED','pager',false,tools.apiURL.interestCouponList,'investList1');
            $('.USED1_a,.EXPIRED_a').removeClass('select');
            $('.UNUSED_a').addClass('select');
            $('.myCoupon_a').addClass('curr');
            $('.myRedPage_a').removeClass('curr');
            $('.investList1,.coupon_s').show();
            $('.investList,.redp_s').hide();
        });
        $('.UNUSED_a').on('click',function () {
            module.initData('UNUSED','pager',false,tools.apiURL.redEnvelopeList,'investList');
            $('.USED_a,.EXPIRED_a').removeClass('select');
            $('.UNUSED_a').addClass('select');
            $('.investList,.redp_s').show();
            $('.investList1,.coupon_s').hide();
        });
        $('.USED_a').on('click',function () {
            module.initData('USED','pager',false,tools.apiURL.redEnvelopeList,'investList');
            $('.EXPIRED_a,.UNUSED_a').removeClass('select');
            $('.USED_a').addClass('select');
            $('.investList,.redp_s').show();
            $('.investList1,.coupon_s').hide();
        });
        $('.EXPIRED_a').on('click',function () {
            module.initData('EXPIRED','pager',false,tools.apiURL.redEnvelopeList,'investList');
            $('.USED_a,.UNUSED_a').removeClass('select');
            $('.EXPIRED_a').addClass('select');
            $('.investList,.redp_s').show();
            $('.investList1,.coupon_s').hide();
        });
        $('.UNUSED1_a').on('click',function () {
            module.initData('UNUSED','pager',false,tools.apiURL.interestCouponList,'investList1');
            $('.USED1_a,.EXPIRED1_a').removeClass('select');
            $('.UNUSED1_a').addClass('select');
            $('.investList1,.coupon_s').show();
            $('.investList,.redp_s').hide();
        });
        $('.USED1_a').on('click',function () {
            module.initData('USED','pager',false,tools.apiURL.interestCouponList,'investList1');
            $('.EXPIRED1_a,.UNUSED1_a').removeClass('select');
            $('.USED1_a').addClass('select');
            $('.investList1,.coupon_s').show();
            $('.investList,.redp_s').hide();
        });
        $('.EXPIRED1_a').on('click',function () {
            module.initData('EXPIRED','pager',false,tools.apiURL.interestCouponList,'investList1');
            $('.USED1_a,.UNUSED1_a').removeClass('select');
            $('.EXPIRED1_a').addClass('select');
            $('.investList1,.coupon_s').show();
            $('.investList,.redp_s').hide();
        });

        $('#investB').hide();
        $('.myCoupon_info').hide();
        $('.myRedPage_info').show();
        $('.coupon_s,.investList1').hide();
        tools.doGet(activityURL + tools.apiURL.redEnvelopeList, {status: "1",pageNumber: "1",pageSize: "10"}, module.totalNum1);
        tools.doGet(activityURL + tools.apiURL.redEnvelopeList, {status: "2",pageNumber: "1",pageSize: "10"}, module.totalNum2);
        tools.doGet(activityURL + tools.apiURL.redEnvelopeList, {status: "3",pageNumber: "1",pageSize: "10"}, module.totalNum3);
        module.initData('UNUSED','pager',true,tools.apiURL.redEnvelopeList,'investList');
    };

    module.totalCoupon1=function (data) {
        $('.UNUSED1_a').text('未使用('+data.data.totalCount+')')
    };
    module.totalCoupon2=function (data) {
        $('.USED1_a').text('已使用('+data.data.totalCount+')')
    };
    module.totalCoupon3=function (data) {
        $('.EXPIRED1_a').text('已过期('+data.data.totalCount+')')
    };
    module.totalNum1=function (data) {
        $('.UNUSED_a').text('未使用('+data.data.totalCount+')')
    };
    module.totalNum2=function (data) {
        $('.USED_a').text('已使用('+data.data.totalCount+')')
    };
    module.totalNum3=function (data) {
        $('.EXPIRED_a').text('已过期('+data.data.totalCount+')')
    };
    module.initData=function (types,pages,tag,urls,bod) {
        var status_s;
        if(types=='UNUSED'){
            status_s='1';
        }else if(types=='USED'){
            status_s='2';
        }else if(types=='EXPIRED'){
            status_s='3';
        }

        $('#'+pages).sjAjaxPager({
            url: activityURL+urls,
            pageSize: 10,
            type :'GET',
            orderField:'gmtCreated',
            orderDirection:'desc',
            searchParam: {
                type : 1,
                status:status_s,
                pageStart:tag
            },
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            beforeSend: function(xhr) {
            },
            success: function (data) {
                var content = data.data.data;
                var list = '',
                    page = '';
                if(content && content.length){
                    list += $.map(content,function(value,len){
                        var cla,sla='',hrefs='';
                        if(types=='UNUSED'){
                            if(bod=='investList'){
                                cla='award-bg-w';
                            }else{
                                cla='award-bg-w interest-bg-w';
                            }
                            sla='立即<br>使用';
                        }else{
                            if(types=='USED'){
                                sla='已<br>使用';
                            }else{
                                sla='已<br>过期';
                            }
                            cla='award-bg-w end';
                        }
                        var  lowLimit='',threshold='';
                        if(value.lowLimit==undefined||value.lowLimit==null||value.lowLimit==''){
                            lowLimit='通用'
                        }else{
                            if(value.upLimit!=null&&value.upLimit!=0){
                                lowLimit=value.lowLimit+'天至'+value.upLimit+'天';
                            }else{
                                lowLimit='项目周期≥'+value.lowLimit+'天';
                            }

                        }
                        if(value.threshold==undefined||value.threshold==null||value.threshold==''){
                            threshold='不限'
                        }else{
                            threshold='单笔投资≥'+value.threshold+'元';
                        }

                        if(bod=='investList'){
                            if(types=='UNUSED'){
                                return '<tr><td><a href="'+goInvest+'" target="_blank"> <em class="'+cla+'"> <tt class="money"> <b>'+value.discount+'</b>元 </tt> <tt class="data"> <b>'+value.expireDate+'</b>过期 </tt> <tt class="mark"> '+sla+' </tt> </em> </a></td><td>'+threshold+'</td> <td>'+lowLimit+'</td> <td> '+value.text+'</td></tr>'
                            }else{
                                return '<tr><td><a> <em class="'+cla+'"> <tt class="money"> <b>'+value.discount+'</b>元 </tt> <tt class="data"> <b>'+value.expireDate.substring(0, 10)+'</b>过期 </tt> <tt class="mark"> '+sla+' </tt> </em> </a></td><td>'+threshold+'</td> <td>'+lowLimit+'</td> <td> '+value.text+'</td></tr>'
                            }
                        }else{
                            var useDays='';
                            if(value.useDays==0){
                                useDays='整个周期';
                            }else{
                                useDays=value.useDays+'天';
                            }
                            if(types=='UNUSED'){
                                return '<tr><td><a href="'+goInvest+'"  target="_blank"> <em class="'+cla+'"> <tt class="money"> <b>'+value.rate+'</b>%</tt> <tt class="data"> <b>'+value.expireDate+'</b>过期 </tt> <tt class="mark"> '+sla+' </tt> </em> </a>' +
                                    '</td><td>'+useDays+'</td> <td>'+threshold+'</td> <td>'+lowLimit+'</td><td> '+value.text+'</td></tr>'

                            }else{
                                return '<tr><td><a> <em class="'+cla+'"> <tt class="money"> <b>'+value.rate+'</b>%</tt> <tt class="data"> <b>'+value.expireDate.substring(0, 10)+'</b>过期 </tt> <tt class="mark"> '+sla+' </tt> </em> </a>' +
                                    '</td><td>'+useDays+'</td> <td>'+threshold+'</td> <td>'+lowLimit+'</td><td> '+value.text+'</td></tr>'

                            }

                        }
                    }).join('');
                    $('#'+pages).show();
                }else{
                    list += '<tr class="noList"><td colspan="5" align="center" style="text-align: center">暂无记录</tr>';
                    $('#'+pages).hide();
                }
                if(bod=='investList') {
                    $('.investList tbody').empty().append(list);
                }else{
                    $('.investList1 tbody').empty().append(list);
                }
            },
            complete: function () {
            }
        });
    };


    module.init();
});